package com.exam.service.impl;

import java.util.List;

import com.exam.dao.ScoreMapper;
import com.exam.dao.UserPaperMapper;
import com.exam.entity.Score;
import com.exam.entity.UserPaper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.exam.dao.QuestionBankMapper;
import com.exam.dao.TestPaperMapper;
import com.exam.entity.TestPaper;
import com.exam.entity.vo.QuestionBankVo;
import com.exam.service.TestPaperService;

@Service
public class TestPaperServiceImpl implements TestPaperService {
	@Autowired
	private TestPaperMapper testPaperMapper;
	@Autowired
	private QuestionBankMapper questionBankMapper; //题库
	@Autowired
	private UserPaperMapper userPaperMapper;
	@Autowired
	private ScoreMapper scoreMapper;


	@Override
	public List<TestPaper> findTestPaperInfo() {

		return testPaperMapper.findTestPaperInfo();
	}

	@Override
	public int updateTestPaperState(int testpaperId) {
		// TODO Auto-generated method stub
		return testPaperMapper.updateTestPaperState(testpaperId);
	}

	@Override
	public TestPaper selectByPrimaryKey(Integer testpaperId) {
		// TODO Auto-generated method stub
		return testPaperMapper.selectByPrimaryKey(testpaperId);
	}

	@Override
	public int updateTestPaperInfo(TestPaper testPaper) {
		// TODO Auto-generated method stub
		return testPaperMapper.updateTestPaperInfo(testPaper);
	}

	@Override
	public List<QuestionBankVo> findAllQuestionBank() {
		
		List<QuestionBankVo> findAllQuestionBank = questionBankMapper.findAllQuestionBank();
		
		return findAllQuestionBank;
	}

	@Override
	public int add(UserPaper userPaper) {
		return userPaperMapper.add(userPaper);
	}

	@Override
	public boolean checkScore(String userId, Integer testpaperId) {
		//查询有没有成绩
		Score score = new Score();
		score.setUsersId(userId);
		score.setTestpaperId(testpaperId);
		Score result_score = scoreMapper.selectFractionByUserIdAndtestPaperId(score);
		//查询试卷是否超时
		TestPaper testPaper = testPaperMapper.selectByPrimaryKey(testpaperId);
		Integer testpaperState = testPaper.getIsStart();
		//没有成绩且考试结束
		if(result_score==null && testpaperState==0){
//			if (result_score.getFraction()==0&&testpaperState==0){
				return false;
//			}
		}

		return true;
	}


}
